<?php

class Model_video extends CI_Model {
    
    function __construct() {
        parent::__construct();
    }

    public function getVideos() {
        $collection = $this->mongo_db->db->selectCollection('video');
        $video = $collection->find();     
        return $video;
    }
    public function getVideo($id) {
        $collection = $this->mongo_db->db->selectCollection('video');
        $video = $collection->findOne(array('_id'=> $id));      
        return $video;
    }

/**
 * ============================UPDATE VIDEO=============================================
*/
    public function update_info($id_vid, $time, $description, $lesson){
        $collection = $this->mongo_db->db->selectCollection('video');
        $data = array(
            '_id' =>$id_vid,
            'time' => $time,
            'description' => $description,
            'lesson' => $lesson
            );
        $collection->update(array('_id' => new MongoId($id_vid)), $data);
    }
/**
 * ============================DELETE VIDEO =============================================
*/
    public function delete_video($id_vid){
        $collection = $this->mongo_db->db->selectCollection('video');
        $collection->remove(array('_id' => new MongoId($id_vid)));
    }

//===============================INSERT NEW VIDEO=============================================
    public function insert_video($id_vid, $cat, $description)
    {
        $collection = $this->mongo_db->db->selectCollection('video');

        $cursor = $collection->findOne(array('_id'=>$id_vid));
        if($cursor) return 'video exists';

        $data = array(
            '_id' =>$id_vid,
            'category' => $cat,
            'description' => $description,
        );
        $insertOptions = array(
            'safe'    => true,
            'fsync'   => true,
            'timeout' => 10000
        );
        try {
            $result = $collection->insert($data);
            return 'Thanh cong';
        }
        catch(MongoCursorException $mce) {
            return 'Thêm video thất bại';
        }
    }
}
?>